//
//  IntersectionShapeScene.h
//  Ray Tracer
//
//  Created by Kaila, Ashish on 4/12/13.
//  Copyright (c) 2013 Kaila, Ashish. All rights reserved.
//

#ifndef Ray_Tracer_IntersectionShapeScene_h
#define Ray_Tracer_IntersectionShapeScene_h

void renderIntersectionShapeScene(Raytracer& raytracer,
                                  int width,
                                  int height,
                                  bool enableSpecular)
{
    // Camera parameters.
	Point3D eye(0, 0, 1);
	Vector3D view(0, 0, -1);
	Vector3D up(0, 1, 0);
	double fov = 60;
    
	// Defines a material for shading.
	SolidMaterial gold( Colour(0.3, 0.3, 0.3), Colour(0.75164, 0.60648, 0.22648),
                       Colour(0.628281, 0.555802, 0.366065),
                       51.2,
                       0,
                       0,
                       0);
    
	SolidMaterial jade( Colour(0, 0, 0), Colour(0.54, 0.89, 0.63),
                       Colour(0.316228, 0.316228, 0.316228),
                       12.8,
                       0,
                       0,
                       0);
    
    SolidMaterial blue( Colour(0, 0, 0.8), Colour(0, 0, 0.8),
                        Colour(0.8, 0.8, 0.8),
                        10,
                        0.5,
                        0.3,
                        0);
    
    SolidMaterial red( Colour(0.8, 0, 0), Colour(0.8, 0, 0),
                      Colour(0.8, 0.8, 0.8),
                      10,
                      0.5,
                      0.3,
                      0.166);
    
	// Defines a point light source.
	raytracer.addLightSource( new PointLight(Point3D(0, 0, 5),
                                             Colour(0.9, 0.9, 0.9) ),
                             enableSpecular);
    
    // Create a soccer by intersecting two spheres
    double sphereScaleFactor[3] = {0.9, 0.2, 0.9};
    UnitSphere* topSphere = new UnitSphere(&jade);
    topSphere->scale(Point3D(), sphereScaleFactor);
    topSphere->translate(Vector3D(0, -0.25, -1));
    
    UnitSphere* bottomSphere = new UnitSphere(&gold);
    bottomSphere->scale(Point3D(), sphereScaleFactor);
    bottomSphere->translate(Vector3D(0, 0.25, -1));
    
    IntersectionShape* soccerShape = new IntersectionShape();
    soccerShape->addChildShape(topSphere);
    soccerShape->addChildShape(bottomSphere);
    
    // Create a semi sphere by intersecting cube with a sphere
    UnitCubeShape* cube = new UnitCubeShape(&blue);
    UnitSphere* sphere = new UnitSphere(&blue);
    double halfSphereScale[3] = {0.5, 0.5, 0.5};
    sphere->scale(Point3D(), halfSphereScale);
    sphere->translate(Vector3D(0, -1, 0));
    IntersectionShape* semiSphere = new IntersectionShape();
    semiSphere->addChildShape(cube);
    semiSphere->addChildShape(sphere);
    semiSphere->translate(Vector3D(0, 0.5, -1));
    
    UnionShape* ufo = new UnionShape();
    ufo->addChildShape(soccerShape);
    ufo->addChildShape(semiSphere);
    raytracer.addObject(ufo);
    
    // Take intersection of 4 spheres
    double distance = 0.5;
    UnitSphere* sphere1 = new UnitSphere(&red);     // Top
    sphere1->translate(Vector3D(0, distance, 0));
    
    UnitSphere* sphere2 = new UnitSphere(&gold);     // Bottom
    sphere2->translate(Vector3D(0, -distance, 0));
    
    UnitSphere* sphere3 = new UnitSphere(&jade);     // Right
    sphere3->translate(Vector3D(-distance, 0, 0));
    
    UnitSphere* sphere4 = new UnitSphere(&blue);     // Left
    sphere4->translate(Vector3D(distance, 0, 0));
    
    IntersectionShape* sphereIntesection1 = new IntersectionShape();
    sphereIntesection1->addChildShape(sphere1);
    sphereIntesection1->addChildShape(sphere2);
    
    IntersectionShape* sphereIntesection2 = new IntersectionShape();
    sphereIntesection2->addChildShape(sphere3);
    sphereIntesection2->addChildShape(sphere4);
    
    UnionShape* sphereIntersectionUnion = new UnionShape();
    sphereIntersectionUnion->addChildShape(sphereIntesection1);
    sphereIntersectionUnion->addChildShape(sphereIntesection2);
    
    sphereIntersectionUnion->translate(Vector3D(1, -2, -5));
    raytracer.addObject(sphereIntersectionUnion);
    
	// Render the scene, feel free to make the image smaller for
	// testing purposes.
	raytracer.render(width, height, eye, view, up, fov, "intersectionshape1.bmp");
	
	// Render it from a different point of view.
    Point3D eye2(4, 2, 1);
	Vector3D view2(-4, -2, -6);
	//raytracer.render(width, height, eye2, view2, up, fov, "unionshape2.bmp");
}

#endif
